import { invokeAsyncAPi } from "@/xz-fn.js";
import modelApi from "@/models/model";
const useDownload = async (signType, info) => {
  const savePath = localStorage.getItem("installPath") || info.installPath;//默认取上一次下载路径
  const parmas = {
    type: signType,
    url: info.signUrl,
  };
  const { code, data } = await modelApi.sign(parmas);
  if (code == 200) {
    let loadUrl = data?.url;
    const params = {
      id: info.id,
      file_name: info.fileName,
      save_path: savePath,
      hash: "",
      url: loadUrl,
    };
    console.log("params", params);
    let res = await invokeAsyncAPi("xz_create_download", params);
    console.log("res", res);
    if (Number(res?.ret) === 0) {
      const conData = {
        id: res.id,
        flag: 0,
        url: loadUrl,
      };
      let respon = await invokeAsyncAPi("xz_ctrl_download", conData);
      if (respon) {
        console.log("下载成功", respon);
        return respon;
      }
    } else {
      return res;
    }
  } else {
    return "获取下载链接失败，请稍后重试";
  }
};

export default useDownload;
